
Date.prototype.setISO8601 = function (string) {
    var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
    "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
    "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
    var d = string.match(new RegExp(regexp));

    var offset = 0;
    var date = new Date(d[1], 0, 1);

    if (d[3]) { 
        date.setMonth(d[3] - 1);
    }
    if (d[5]) { 
        date.setDate(d[5]);
    }
    if (d[7]) { 
        date.setHours(d[7]);
    }
    if (d[8]) { 
        date.setMinutes(d[8]);
    }
    if (d[10]) { 
        date.setSeconds(d[10]);
    }
    if (d[12]) { 
        date.setMilliseconds(Number("0." + d[12]) * 1000);
    }
    if (d[14]) {
        offset = (Number(d[16]) * 60) + Number(d[17]);
        offset *= ((d[15] == '-') ? 1 : -1);
    }

    offset -= date.getTimezoneOffset();
    time = (Number(date) + (offset * 60 * 1000));
    this.setTime(Number(time));
}


var timer;
var twiter_friend_count=0;
var twitter_firend_array=new Array();
var bool_get_chart=true;
var total_count=0;
var func_end=false;
var chart_color=new Array("802B00","806A00","2B0080","6A0080","558000","7C00DB","336699","88C700","999933","CC9900");
function twitterfirend()
{
    this.name="";
    this.speed=0.0;
}
function getHealth(index)
{
    var cur=parseInt(twitter_firend_array[index].speed*10)
    if(cur==0)
    {
        return 0;
    }
    else
    {
        return 10*Math.log(cur);
    }
}
function getChartURL(StartIndex)
{
    chart_color.sort(function() {
        return 0.5 - Math.random()
        });
    var end=StartIndex+10;
    var auto_size=true;
    if(twiter_friend_count<StartIndex+10)
    {
        end=twiter_friend_count;
        
    }
    //http://chart.apis.google.com/chart?cht=bvg&chs=1000x200&chd=t:20|30|40&chdl=aaa|bbb|ccc&chco=fff0f0,ff00f0,ff0000
    var chart_url="http://chart.apis.google.com/chart?";
    chart_url+="cht=bvg&";
    chart_url+=("chs=500x200&");
    chart_url+="chd=t:";
    
    //CHART DATA
    chart_url+=getHealth(StartIndex);
    for(var i=StartIndex+1;i<end;i++)
    {
        chart_url+="|";
        chart_url+=getHealth(i);
    }
    
    //CHART LABEL
    chart_url+="&chdl=";
    chart_url+=(twitter_firend_array[StartIndex].name);
    for(var i=StartIndex+1;i<end;i++)
    {
        chart_url+="|";
        chart_url+=twitter_firend_array[i].name;

    }
    
    
    chart_url+="&chco=";
    chart_url+=chart_color[StartIndex%chart_color.length];
    for(var i=StartIndex+1;i<end;i++)
    {
        chart_url+=",";
        chart_url+=chart_color[i%chart_color.length];
    }
    if(total_count>1)
    {
        chart_url+="&chbh=a";
    }
    else
    {
        chart_url+="&chbh=150";
        chart_url+="&chtt="+twitter_firend_array[StartIndex].name+"'s|TweetActive|Index+is|"+parseInt(getHealth(StartIndex));
    }
    return chart_url;
}
function getchart()
{
    if((twiter_friend_count>=total_count || func_end) && bool_get_chart && total_count!=0)
    {
        var chart_url="";
        twitter_firend_array.sort(function(a,b){
            return b.speed - a.speed
            });
        for(var i=0;i<twiter_friend_count;i+=10)
        {
            chart_url=getChartURL(i);
            var div=document.getElementById("SPEED");
            var chartimage=document.createElement("img");
            chartimage.setAttribute("id","CHART");
            chartimage.setAttribute("src",chart_url);
            var ln_br1=document.createElement("br");
               
            var ln_br2=document.createElement("br");
            div.appendChild(chartimage);
            div.appendChild(ln_br1);
               
            if(twiter_friend_count==1)
            {
                var a_link=document.createElement("a");
                var href_url="http://twitter.com/home?status=";
                href_url+="My+Current+Activity+Index+is+"+parseInt(getHealth(0));
                href_url+="+http%3a%2f%2ftweetactive.com";
                a_link.setAttribute("href",href_url);
                a_link.innerHTML="Tweet This";
                div.appendChild(a_link);
            }
            div.appendChild(ln_br2);
        }
        bool_get_chart=false;
        clearInterval(timer);
        var wait_div=document.getElementById("WAIT");
        wait_div.innerHTML="";
           
    }
    else if(twiter_friend_count>0)
    {
        func_end=true;
    }

}
function gettimediff(isotime)
{
    var d1=new Date();
    var d2 = new Date();
    d2.setISO8601(isotime);
    var convert=24*60*60*1000;
    var d=(d1-d2)/convert;
    return d;
}
function cbfunc(res)
{
    if(res.query.count>0)
    {
        //var index=res.query.count-1;
        var d1=new Date();
        var d2 = new Date();
        var speed_max=0;
        if(res.query.count>1)
        {
            //d2.setISO8601(res.query.results.span[index].title);
            for(var i=0;i<res.query.count;i++)
            {
                var cur_speed=(i+1)/gettimediff(res.query.results.span[i].title);
                if(speed_max<cur_speed)
                {
                    speed_max=cur_speed;
                }
            }
        }
        else if(res.query.count==1)
        {
            speed_max=1/gettimediff(res.query.results.span.title);
        //d2.setISO8601(res.query.results.span.title);
        }
        var div=document.getElementById("SPEED");
        var user_img=document.createElement("img");
        var username=res.query.diagnostics.url.content;
        if(!username)
        {
            username=res.query.diagnostics.url[1].content;
        }
        var twitter_url="http://twitter.com/";
        username=username.substring(twitter_url.length);
        twitter_firend_array[twiter_friend_count]=new twitterfirend();
        twitter_firend_array[twiter_friend_count].name=username;
        twitter_firend_array[twiter_friend_count].speed=speed_max;
        twiter_friend_count++;
        //user_img.src= "http://chart.apis.google.com/chart?cht=gom&chs=500x100&chd=t:"+((res.query.count/d)*10)+"&chl="+username;
        //div.appendChild(user_img);
        //var lbreak=document.createElement("br");
        //div.appendChild(lbreak);
        func_end=false;
    }
}
function seed(userid)
{
    var div=document.getElementById("SCRIPT");
    var scrpt=document.createElement("script");
    //select * from html where url="http://twitter.com/markandey" and xpath='//span[@class="published"]'
    scrpt.src="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Ftwitter.com%2F"+userid+"%22%20and%20xpath%3D'%2F%2Fspan%5B%40class%3D%22published%22%5D'&format=json&callback=cbfunc";
    div.appendChild(scrpt);
}
function getfriends(res)
{
    if(res!=null)
    {
        total_count=res.length;
        for(var i=0;i<res.length;i++)
        {
            twitter_firend_array[i]=new twitterfirend();
            seed(res[i].screen_name);
        }
        timer=setInterval(getchart,2000);
    }
    else
    {
        alert("limit exceeded ");
    }
}
function init_variables()
{
    bool_get_chart=true;
    total_count=0;
    func_end=false;
    clearInterval(timer);
    twiter_friend_count=0;
    twitter_firend_array=new Array();
    bool_get_chart=true;
    var wait_div=document.getElementById("WAIT");
    wait_div.innerHTML="<"+"span style=\"font-size: 16pt; color: #ff0000\"><"+"strong"+">"+"Loading... </"+"strong></"+"span>";
        
    
}
function seedonlyme()
{

    init_variables();
    var twitteridbox=document.getElementById("TWITTERID");
    var userid = twitteridbox.value
    total_count=1;
    var products = twitteridbox.value;
    var product_list=products.split(' ');
    total_count=product_list.length;
    twitter_firend_array[0]=new twitterfirend()
    for(var i=0;i<product_list.length;i++)
    {
        seed(product_list[i]);
    }
    //seed(userid);
    timer=setInterval(getchart,2000);
    return false;
}
function seedscript()
{
    init_variables();
    var QueryString = location.search;
    var twitteridbox=document.getElementById("TWITTERID");
    var div=document.getElementById("SCRIPT");
    var userid = twitteridbox.value
    //QueryString.substring(QueryString.indexOf("id=") + 3, QueryString.length);
    var scrpt=document.createElement("script");
    //http://twitter.com/statuses/friends/markandey.json?callback=hi
    scrpt.src="http://twitter.com/statuses/friends/"+userid+".json?callback=getfriends"
    div.appendChild(scrpt);
    seed(userid);
}
